package com.woniuxy.qiantai.mq;

import com.rabbitmq.client.Channel;
import com.woniuxy.qiantai.mapper.OrderMapper;
import com.woniuxy.qiantai.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;



@Slf4j
@Component
//用户死信处理
public class UserDeadListener {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderMapper orderMapper;

    //处理死信队列
    @RabbitListener(queues = "userDeadQueue")
    public void detailDeadMessage(Message message, Channel channel, Integer orderId) throws IOException {
        //自动ack的时候,如果方法正常执行,就确认消息被消费
        //获取用户订单状态
        Integer status = orderMapper.selectById(orderId).getStatus();
        System.out.println("判断用户当前订单状态："+status);
        //判断用户是否支付
        if (status==0){
            try {
                System.out.println(orderId+"订单以取消");

                orderService.updateOrderState(orderId,2);

                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

            } catch (Exception e) {
                e.printStackTrace();
        }
        }else {
            System.out.println(orderId+"订单已支付");
        }
        //自动ack的时候,监听方法里不要抛出异常,否则会无限重试,导致死循环
        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        //channel.basicConsume(MqDelayConfig.DEAD_QUEUE,false,detailDeadMessage();)


    }
}
